﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YYSite.Common
{
    /// <summary>
    /// 分页条件
    /// </summary>
    public class DataGridInput
    {
        public const string ASC = " ASC ";

        public const string DESC = " DESC ";

        public const string PK = " Id ";
        /// <summary>
        /// 当前页
        /// </summary>
        public int Page { get; set; }
        /// <summary>
        /// 分页大小
        /// </summary>
        public int Rows { get; set; }
        /// <summary>
        /// 关键词搜索
        /// </summary>
        public string Keywords { get; set; }
        /// <summary>
        /// 排序字段
        /// </summary>
        public virtual string Sort { get; set; }
        /// <summary>
        /// 排序方向
        /// </summary>
        public virtual string Order { get; set; }

        /// <summary>
        /// 参数化查询参数
        /// </summary>
        protected SqlDynamicParameters DbParameters { get; }
        /// <summary>
        /// 查询条件
        /// </summary>
        protected StringBuilder DbSQL { get; }

        /// <summary>
        /// 构建条件语句和参数
        /// </summary>
        public virtual void Initialize()
        {
            //例子：
            //if (!string.IsNullOrWhiteSpace(this.Keywords))
            //{
            //    this.DbSQL.Append(" AND UserName LIKE @Keywords");
            //    this.DbParameters.Add("@Keywords", "%" + this.Keywords + "%", System.Data.DbType.String);
            //}
        }
        /// <summary>
        /// 获取查询参数
        /// </summary>
        /// <returns></returns>
        public virtual SqlDynamicParameters GetParameters()
        {
            return this.DbParameters;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        public virtual string GetSQL()
        {
            return this.DbSQL.ToString();
        }


        public DataGridInput()
        {
            this.Page = 1;
            this.Rows = 10;
            this.Sort = PK;
            this.Order = ASC;
            this.DbParameters = new SqlDynamicParameters();
            this.DbSQL = new StringBuilder(100);
            this.DbSQL.Append(" 1 = 1 ");
        }
        /// <summary>
        /// 获取开始记录数
        /// </summary>
        /// <returns></returns>
        public int GetStartRecordIndex()
        {
            int start = (this.Page - 1) * this.Rows + 1;
            return start < 1 ? 1 : start;
        }
        /// <summary>
        /// 获取结束记录数
        /// </summary>
        /// <returns></returns>
        public int GetEndRecordIndex()
        {
            int end = this.Page * this.Rows;
            if (end < 1)
                end = this.Rows;
            return end;
        }
        /// <summary>
        /// 获取当前页
        /// </summary>
        /// <returns></returns>
        public int GetPageIndex()
        {
            return this.Page;
        }
        /// <summary>
        /// 获取分页大小
        /// </summary>
        /// <returns></returns>
        public int GetPageSize()
        {
            return this.Rows;
        }

       public  string GetOrderBy()
        {
            if (string.IsNullOrWhiteSpace(this.Sort))
                this.Sort = PK;
            return this.Sort + " " + this.Order;
        }
    }
}
